package cn.org.xiaosheng.resttemplate;


import cn.org.xiaosheng.resttemplate.utils.PostUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author XiaoSheng
 * @date 2023-11-17
 * @dec 描述
 */
@RestController
public class PostRestTempTest {

    @Resource
    private PostUtils postUtils;

    /**
     * 测试post请求, 入参方式为form-data或者application/x-www-form-urlencoded方式
     * @return
     */
    @RequestMapping("/postRestTempTest")
    public String postTest() {
        MultiValueMap<String, String> bodyMap = new LinkedMultiValueMap<>();
        bodyMap.add("appKey", "bee95axxxxxdc0f1e");
        bodyMap.add("appSecret", "ddc77xxxxxf890bb510");
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        JSONObject post = postUtils.requestFormData("https://xxxx/api/lapp/token/get", bodyMap, null);
        System.out.println(JSONObject.toJSONString(post));
        return JSONObject.toJSONString(post);
    }

    /**
     * 测试post请求, 入参方式为form-data或者application/x-www-form-urlencoded方式
     * 测试发现, 即使将其Headers头变更为x-www-form-urlencoded 依旧传递不了From-data参数至三方请求接口
     * 所以该方法无法成为入参为application/x-www-form-urlencoded方式的请求方案!
     * @return
     */
    @RequestMapping("/postRestTempTest2")
    public String postTest2() {
        Map<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("appKey", "bee9xxxxddc0f1e");
        bodyMap.put("appSecret", "ddc77xxxx90bb510");
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        ResponseEntity<JSONObject> post = postUtils.requestObject("https://xxxx/api/lapp/token/get", HttpMethod.POST, bodyMap, null);
        System.out.println(post.getBody().toJSONString());
        return post.getBody().toJSONString();
    }

    /**
     * 测试JSON请求方式 多级嵌套方式
     * 具体报文:
     * {
     *     "agentId": "eba5652793ee462f8cf2e78109e49326",
     *     "deviceid": "3C6A2CFFFED345C4",
     *     "accessToken": "2AHxxxxJkKp",
     *     "topic": "api/device/3C6A2CFFFED345C4/cloud",
     *     "dtype": 1,
     *     "sdata": "{\"type\":\"zigbeeConfig\",\"command\":\"epList\",\"session\":\"\",\" \":1024}",
     *     "ts": "586851710"
     * }
     * @return
     */
    @RequestMapping("/postRestTempTest1")
    public String postTest1() {
        HashMap<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("agentId", "eba5652793ee462f8cf2e78109e49326");
        bodyMap.put("deviceid", "3C6A2CFFFED345C4");
        bodyMap.put("accessToken", "2AHxxxJkKp");
        bodyMap.put("topic", "api/device/3C6A2CFFFED345C4/cloud");
        bodyMap.put("dtype", "1");
        bodyMap.put("ts", "586851710");
        Map<String, Object> sData = new HashMap<String, Object>();
        sData.put("type","zigbeeConfig");
        sData.put("command", "epList");
        sData.put("session", "");
        sData.put("sequence", "1024");
        bodyMap.put("sdata", JSONObject.toJSONString(sData));
        // 可以不需要传递请求头, 会根据你传入的川参加进行判断,从而确定是哪种请求头
        ResponseEntity<JSONObject> responseEntity = postUtils.postRequest("http://xxxx/owon-data/5000/agent/sendMqttData", null, bodyMap);
        return responseEntity.getBody().toJSONString();
    }


}
